package com.kpx.app.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.kpx.app.R;
import com.kpx.app.adapter.MessageAdapter;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.bean.MessageBean;
import com.kpx.app.bean.MessageMainBean;
import com.kpx.app.http.HttpDataApi;
import com.kpx.app.http.HttpDataLoader;
import com.kpx.app.http.HttpDataReqUrl;
import com.kpx.app.http.HttpDataRequest;
import com.kpx.app.http.ResObj;
import com.kpx.app.utils.MyHashMap;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by congdy on 17/3/7.
 * 消息列表
 */

public class ActivityMessgeList extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.lv_show)
    ListView lvShow;
    @BindView(R.id.out_view)
    XRefreshView outView;
    @BindView(R.id.txt_empty_mention)
    TextView txtEmptyMention;
    private MessageAdapter adapter;
    private List<MessageMainBean> messageslist = new ArrayList<>();
    private int[] icons = new int[]{R.drawable.msg_type_sys, R.drawable.msg_type_order, R.drawable.msg_type_qianbao, R.drawable.msg_type_task};

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_message_list);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "消息列表");
        UIUtils.initRefreshView(outView);
        getMessage();// 获取数据
    }

    @Override
    public void initData() {
        //商品列表的上下拉刷新操作
        outView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh() {
                getMessage();// 获取数据
            }

            @Override
            public void onLoadMore(boolean isSlience) {
                outView.setLoadComplete(true);
            }
        });
        adapter = new MessageAdapter(ActivityMessgeList.this, messageslist, R.layout.item_message_list);
        lvShow.setAdapter(adapter);
        lvShow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ActivityMessageShow.msgType = messageslist.get(position).getValue();
                IntentUtil.gotoActivity(ActivityMessgeList.this, ActivityMessageShow.class);
            }
        });
    }

    /**
     * 获取消息列表
     */
    private void getMessage() {
        MyHashMap map = new MyHashMap();
        new HttpDataLoader(new HttpDataApi.OnRequestCallback() {
            @Override
            public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {
                outView.stopRefresh();
                if (result.getCode() == 0) {
                    if (result.getData() != null) {
                        messageslist.addAll((ArrayList<MessageMainBean>) result.getData());
                        if (messageslist.size() > 0) {
                            for (int i = 0; i < messageslist.size(); i++) {
                                messageslist.get(i).setIcon(icons[messageslist.get(i).getValue() - 1]);
                            }
                            adapter.setData(messageslist);
                            txtEmptyMention.setVisibility(View.GONE);
                        } else {
                            txtEmptyMention.setVisibility(View.VISIBLE);
                        }

                    }
                } else {
                    ToastUtil.getInstance().showToast(ActivityMessgeList.this, result.getMsg());
                }
            }

            @Override
            public void onRequestError(String url, int statusCode, String error) {
                outView.stopRefresh();
                ToastUtil.getInstance().showToast(ActivityMessgeList.this, error);
            }
        }, MessageMainBean.class, HttpDataRequest.TYPE_LIST).POST(HttpDataReqUrl.URL_POST_GET_MESSAGE_LIST, true, map);
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }
}
